package bday.tvlport.beans;

import java.util.ArrayList;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.model.ListDataModel;

import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import bday.tvlport.db.DbConnection;
import bday.tvlport.models.UserDetails;
import bday.tvlport.utils.DobUtils;

@ManagedBean(name="fetchdata")
@RequestScoped
public class FetchDataController {

	private String sort;
	private String order;
	private ListDataModel<UserDetails> usersDataModel;
	
	public FetchDataController() {
		
	}
		
	public ListDataModel<UserDetails> getAllUsers() {
		populateUsers();
		return usersDataModel;
	}

	private void populateUsers() {
		usersDataModel = new ListDataModel<UserDetails>();
		MongoOperations mongoOperations = DbConnection.getMongo();
		if(mongoOperations != null) {
			Sort sorting = new Sort(Direction.ASC, "name", "dobMonthDate");
			if("DOB".equals(sort)) {
				if("Desc".equals(order)) {
					sorting = new Sort(Direction.DESC, "dobMonthDate");
				} else {
					sorting = new Sort(Direction.ASC, "dobMonthDate");
				}
				sorting = sorting.and(new Sort(Direction.ASC, "name"));
			} else if ("Name".equals(sort) && "Desc".equals(order)) {
				sorting = new Sort(Direction.DESC, "name").and(new Sort(Direction.ASC, "dobMonthDate"));
			}
			
			Query searchUserQuery = new Query().with(sorting);
			List<UserDetails> list = mongoOperations.find(searchUserQuery, UserDetails.class);
			usersDataModel.setWrappedData(list);
		}
	}
	
	public void handleChoiceChange() {
		populateUsers();
	}
	
	public ListDataModel<UserDetails> getUpcomingUsers() {
		ListDataModel<UserDetails> usersDataModel = new ListDataModel<UserDetails>();
		MongoOperations mongoOperations = DbConnection.getMongo();
		if(mongoOperations != null) {
			Sort sorting = new Sort(Direction.ASC, "dobMonthDate", "name");
			long currentTimeMillis = System.currentTimeMillis();
			//2weeks 14*24*3600*1000 = 1209600000 millis
			Integer start = DobUtils.getMonthDate(currentTimeMillis);
			Integer end = DobUtils.getMonthDate(currentTimeMillis+1209600000);
			List<UserDetails> results = new ArrayList<UserDetails>();
			Query searchUserQuery = null;
			Criteria criteria = null;
			if(start > end) {
				criteria = Criteria.where("dobMonthDate").gte(start);
				searchUserQuery = new Query(criteria).with(sorting);
				results.addAll(mongoOperations.find(searchUserQuery, UserDetails.class));
				criteria = Criteria.where("dobMonthDate").lte(end);
				searchUserQuery = new Query(criteria).with(sorting);
				results.addAll(mongoOperations.find(searchUserQuery, UserDetails.class));
			} else {
				criteria = new Criteria().andOperator(Criteria.where("dobMonthDate").gte(start),
						(Criteria.where("dobMonthDate").lte(end)));
				searchUserQuery = new Query(criteria).with(sorting);
				results.addAll(mongoOperations.find(searchUserQuery, UserDetails.class));
			}
			usersDataModel.setWrappedData(results);
		}
		return usersDataModel;
	}

	public String getSort() {
		return "Asc";
	}

	public void setSort(String sort) {
		this.sort = sort;
	}

	public String getOrder() {
		return "Name";
	}

	public void setOrder(String order) {
		this.order = order;
	}

	public ListDataModel<UserDetails> getUsersDataModel() {
		return usersDataModel;
	}

	public void setUsersDataModel(ListDataModel<UserDetails> usersDataModel) {
		this.usersDataModel = usersDataModel;
	}
}
